package com.qinglian.controller;

import com.qinglian.utils.FileUtil;
import com.qinglian.utils.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@CrossOrigin
public class FileController {

    @Value("${url}")
    private String url;

    @PostMapping("/uploadImg")
    public Result<?> img(@RequestParam(value = "file") MultipartFile file,Integer type,String name){

        if (type == 1) {
            name = "userId-" + name + ".png";
        } else {
            String uuid = UUID.randomUUID().toString().trim().replaceAll("-","");
            name = uuid + ".png";
        }
        try{
            return Result.success(FileUtil.uploadImg(file,
                    "D:\\Users\\Augenstern\\Desktop\\qingLian\\qingLian\\src\\main\\resources\\static\\image\\",
                    name,
                    "http://" + url +":9090/image"));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("-1","图片上传失败");
        }
    }
}